package com.yiren.backstage.judge.services.standard.impl;

import android.content.Context;

import com.yiren.backstage.currency.bean.VoiceBean;
import com.yiren.backstage.currency.constant.AutoJudgeConstant;
import com.yiren.backstage.currency.entity.GradeStandardEntity;
import com.yiren.backstage.currency.entity.JudgeDetailsEntity;
import com.yiren.backstage.currency.entity.ProjectEntity;
import com.yiren.backstage.currency.enums.TaskStateEnum;
import com.yiren.backstage.currency.utils.MessageUtil;
import com.yiren.backstage.currency.utils.StringUtil;
import com.yiren.backstage.judge.api.impl.ExamJudgeApiImpl;
import com.yiren.backstage.judge.bean.NodeBean;
import com.yiren.backstage.judge.bean.TreeBean;
import com.yiren.backstage.judge.cache.JudgeTreeCacheFacade;
import com.yiren.backstage.judge.handler.AutoJudgeHandler;
import com.yiren.backstage.judge.services.standard.IAutoJudgeHandlerService;
import com.yiren.backstage.judge.thread.AutoJudgeThread;
import com.yiren.backstage.judge.thread.CachedThreadPoolManage;
import com.yiren.backstage.judge.thread.JjdJudgeThread;
import com.yiren.backstage.judge.thread.SczbJudgeThread;
import com.yiren.backstage.player.api.IPlayerCallBack;
import com.yiren.backstage.player.api.PlayerApi;

/**
 * Time:2021/11/12 16:02
 * Author:xiaozan
 * Description:自动评判消息处理中心
 */
public class AutoJudgeHandlerServiceImpl implements IAutoJudgeHandlerService {

    private Context context;

    private AutoJudgeHandler autoJudgeHandler;

    public AutoJudgeHandlerServiceImpl(Context context, AutoJudgeHandler autoJudgeHandler) {
        this.context = context;
        this.autoJudgeHandler = autoJudgeHandler;
    }

    @Override
    public void playProjectStartVoice(final ProjectEntity projectEntity) {
        //上车准备无项目开始语音
        if (projectEntity.getId().equals("40100")) {
            CachedThreadPoolManage.getInstance().startThread(new SczbJudgeThread(context, autoJudgeHandler, projectEntity));
            return;
        }

        //项目开始播报语音为空
        if (StringUtil.isEmpty(projectEntity.getStartContent()) && StringUtil.isEmpty(projectEntity.getStartVoiceName())) {
            if (projectEntity.getId().equals("40400")) {
                //启动加减档评判
                CachedThreadPoolManage.getInstance().startThread(new JjdJudgeThread(context, autoJudgeHandler, projectEntity));
            } else {
                autoJudgeHandler.sendMessage(MessageUtil.newMessage(AutoJudgeConstant.START_PROJECT_JUDGE, projectEntity));
            }
            return;
        }

        VoiceBean voiceBean = new VoiceBean(projectEntity.getStartContent(), projectEntity.getStartVoiceName());

        autoJudgeHandler.getiExamStateCallBack().playProjectVoice(voiceBean);

        PlayerApi.playProjectAndJudgeVoice(context, voiceBean, new IPlayerCallBack() {
            @Override
            public void playFinish() {
                //播报完成后启动评判
                if (projectEntity.getId().equals("40400") ) {
                    CachedThreadPoolManage.getInstance().startThread(new JjdJudgeThread(context, autoJudgeHandler, projectEntity));
                } else {
                    autoJudgeHandler.sendMessage(MessageUtil.newMessage(AutoJudgeConstant.START_PROJECT_JUDGE, projectEntity));
                }
            }
        });
    }

    @Override
    public void startProjectJudge(final ProjectEntity projectEntity) {
        TreeBean treeBean = JudgeTreeCacheFacade.getTaskTreeCache(projectEntity.getId());
        if (null == treeBean || treeBean.getTreeNodes().size() == 0) {
            //没有评分结构，播报结束语音
            autoJudgeHandler.sendMessage(MessageUtil.newMessage(AutoJudgeConstant.PLAY_PROJECT_END_VOICE, projectEntity));
            return;
        }
        //实例当前项目根父级线程
        AutoJudgeThread rootAutoJudgeThread = new AutoJudgeThread(context, projectEntity, autoJudgeHandler);
        rootAutoJudgeThread.setmRootTask(rootAutoJudgeThread);
        rootAutoJudgeThread.setSumNodeCount(treeBean.getTreeNodesSumCount());
        rootAutoJudgeThread.setmTaskNode(null);

        //遍历父级ID为0的节点启动
        for (int i = 0; i < treeBean.getTreeNodes().size(); i++) {
            NodeBean node = treeBean.getTreeNodes().get(i);
            if (node.getParentId().equals("0")) {
                final AutoJudgeThread autoJudgeThread = new AutoJudgeThread(context, projectEntity, autoJudgeHandler);
                //赋值当前任务节点
                autoJudgeThread.setmTaskNode(node);
                //赋值当前根任务
                autoJudgeThread.setmRootTask(rootAutoJudgeThread);
                //播报任务详情语音
                JudgeDetailsEntity judgeDetailsEntity = (JudgeDetailsEntity) node.getEntity();
                //任务详情无开始播报语音
                if (StringUtil.isEmpty(judgeDetailsEntity.getStartContent()) && StringUtil.isEmpty(judgeDetailsEntity.getStartVoiceName())) {
                    CachedThreadPoolManage.getInstance().startThread(autoJudgeThread);
                    continue;
                }

                VoiceBean voiceBean = new VoiceBean(judgeDetailsEntity.getStartContent(), judgeDetailsEntity.getStartVoiceName());

                autoJudgeHandler.getiExamStateCallBack().playProjectVoice(voiceBean);

                PlayerApi.playProjectAndJudgeVoice(context, voiceBean, new IPlayerCallBack() {
                    @Override
                    public void playFinish() {
                        //播报完成后启动评判
                        CachedThreadPoolManage.getInstance().startThread(autoJudgeThread);
                    }
                });
            }
        }
    }

    @Override
    public void startWholeCourseProjectJudge(ProjectEntity projectEntity) {
        TreeBean treeBean = JudgeTreeCacheFacade.getTaskTreeCache(projectEntity.getId());
        if (null == treeBean || treeBean.getTreeNodes().size() == 0) {
            //没有评分结构，播报结束语音
            autoJudgeHandler.sendMessage(MessageUtil.newMessage(AutoJudgeConstant.PLAY_PROJECT_END_VOICE, projectEntity));
            return;
        }
        //遍历父级ID为0的节点启动
        for (int i = 0; i < treeBean.getTreeNodes().size(); i++) {
            NodeBean node = treeBean.getTreeNodes().get(i);
            if (node.getParentId().equals("0")) {
                final AutoJudgeThread wholeCourseJudgeThread = new AutoJudgeThread(context, projectEntity, autoJudgeHandler);
                //赋值当前任务节点
                wholeCourseJudgeThread.setmTaskNode(node);
                //赋值当前根任务 根任务为自己
                wholeCourseJudgeThread.setmRootTask(wholeCourseJudgeThread);
                nodeSumCount(wholeCourseJudgeThread, node);
                //播报任务详情语音
                JudgeDetailsEntity judgeDetailsEntity = (JudgeDetailsEntity) node.getEntity();

                //任务详情无开始播报语音
                if (StringUtil.isEmpty(judgeDetailsEntity.getStartContent()) && StringUtil.isEmpty(judgeDetailsEntity.getStartVoiceName())) {
                    CachedThreadPoolManage.getInstance().startThread(wholeCourseJudgeThread);
                    continue;
                }
                PlayerApi.playProjectAndJudgeVoice(context, new VoiceBean(judgeDetailsEntity.getStartContent(), judgeDetailsEntity.getStartVoiceName()), new IPlayerCallBack() {
                    @Override
                    public void playFinish() {
                        //播报完成后启动评判
                        CachedThreadPoolManage.getInstance().startThread(wholeCourseJudgeThread);
                    }
                });
            }
        }
    }

    @Override
    public void againExecuteJudge(final AutoJudgeThread wholeCourseJudgeThread) {
        NodeBean gradeNode = wholeCourseJudgeThread.getmRootTask().getmTaskNode();
        AutoJudgeThread againWholeCourseJudgeThread = new AutoJudgeThread(context, wholeCourseJudgeThread.getProjectEntity(), autoJudgeHandler);
        //赋值当前任务节点
        againWholeCourseJudgeThread.setmTaskNode(gradeNode);
        //赋值当前根任务 根任务为自己
        againWholeCourseJudgeThread.setmRootTask(againWholeCourseJudgeThread);
        nodeSumCount(againWholeCourseJudgeThread, gradeNode);
        //播报任务详情语音
        JudgeDetailsEntity judgeDetailsEntity = (JudgeDetailsEntity) gradeNode.getEntity();
        //任务详情无开始播报语音
        if (StringUtil.isEmpty(judgeDetailsEntity.getStartContent()) && StringUtil.isEmpty(judgeDetailsEntity.getStartVoiceName())) {
            CachedThreadPoolManage.getInstance().startThread(againWholeCourseJudgeThread);
            return;
        }
        PlayerApi.playProjectAndJudgeVoice(context, new VoiceBean(judgeDetailsEntity.getStartContent(), judgeDetailsEntity.getStartVoiceName()), new IPlayerCallBack() {
            @Override
            public void playFinish() {
                //播报完成后启动评判
                CachedThreadPoolManage.getInstance().startThread(againWholeCourseJudgeThread);
            }
        });
    }

    @Override
    public void continueExecuteJudge(final AutoJudgeThread autoJudgeThread) {
        NodeBean gradeNode = autoJudgeThread.getmTaskNode();
        for (NodeBean nodeBean : gradeNode.getChildren()) {
            JudgeDetailsEntity judgeDetailsEntity = (JudgeDetailsEntity) nodeBean.getEntity();
            final AutoJudgeThread childAutoJudgeThread = new AutoJudgeThread(context, autoJudgeThread.getProjectEntity(), autoJudgeHandler);
            childAutoJudgeThread.setmTaskNode(nodeBean);
            childAutoJudgeThread.setmRootTask(autoJudgeThread.getmRootTask());
            //播报任务详情语音
            //任务详情无开始播报语音
            if (StringUtil.isEmpty(judgeDetailsEntity.getStartContent()) && StringUtil.isEmpty(judgeDetailsEntity.getStartVoiceName())) {
                CachedThreadPoolManage.getInstance().startThread(childAutoJudgeThread);
                continue;
            }

            VoiceBean voiceBean = new VoiceBean(judgeDetailsEntity.getStartContent(), judgeDetailsEntity.getStartVoiceName());

            autoJudgeHandler.getiExamStateCallBack().playProjectVoice(voiceBean);

            PlayerApi.playProjectAndJudgeVoice(context, voiceBean, new IPlayerCallBack() {
                @Override
                public void playFinish() {
                    //播报完成后启动评判
                    CachedThreadPoolManage.getInstance().startThread(childAutoJudgeThread);
                }
            });
        }
    }

    @Override
    public void addGradeStandard(GradeStandardEntity gradeStandardEntity) {
        autoJudgeHandler.getiExamStateCallBack().addGradeStandard(gradeStandardEntity);
    }

    @Override
    public void playProjectEndVoice(final ProjectEntity projectEntity) {
        //无项目结束播报语音
        if (StringUtil.isEmpty(projectEntity.getEndContent()) && StringUtil.isEmpty(projectEntity.getEndVoiceName())) {
            projectEntity.setTaskState(TaskStateEnum.FINISH);
            ExamJudgeApiImpl.projectStateMap.put(projectEntity.getId(), projectEntity);
            //将项目执行状态改成执行完成
            autoJudgeHandler.getiExamStateCallBack().projectState(projectEntity, TaskStateEnum.FINISH);
            return;
        }

        VoiceBean voiceBean = new VoiceBean(projectEntity.getEndContent(),projectEntity.getEndVoiceName());

        autoJudgeHandler.getiExamStateCallBack().playProjectVoice(voiceBean);

        PlayerApi.playProjectAndJudgeVoice(context, voiceBean, new IPlayerCallBack() {
            @Override
            public void playFinish() {
                projectEntity.setTaskState(TaskStateEnum.FINISH);
                ExamJudgeApiImpl.projectStateMap.put(projectEntity.getId(), projectEntity);
                //将项目执行状态改成执行完成
                autoJudgeHandler.getiExamStateCallBack().projectState(projectEntity, TaskStateEnum.FINISH);
            }
        });
    }

    /**
     * 获取全程评判任务详情条数
     *
     * @param autoJudgeThread
     * @param node
     */
    private void nodeSumCount(AutoJudgeThread autoJudgeThread, NodeBean node) {
        autoJudgeThread.setSumNodeCount(autoJudgeThread.getSumNodeCount() + 1);
        for (int i = 0; i < node.getChildren().size(); i++) {
            NodeBean currentNode = node.getChildren().get(i);
            nodeSumCount(autoJudgeThread, currentNode);
        }
    }
}
